﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace BLL
{
    public class ePioneer_Reservation
    {
        private readonly DAL.ePioneer_Reservation dal = new DAL.ePioneer_Reservation();
        public ePioneer_Reservation()
        { }
        #region  Method

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(Model.ePioneer_Reservation model)
        {
            return dal.Add(model);
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Model.ePioneer_Reservation model)
        {
            return dal.Update(model);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int Id)
        {

            return dal.Delete(Id);
        }
        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool DeleteList(string Idlist)
        {
            return dal.DeleteList(Idlist);
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Model.ePioneer_Reservation GetModel(int Id)
        {

            return dal.GetModel(Id);
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            return dal.GetList(strWhere);
        }
        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            return dal.GetList(Top, strWhere, filedOrder);
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<Model.ePioneer_Reservation> GetModelList(string strWhere)
        {
            DataSet ds = dal.GetList(strWhere);
            return DataTableToList(ds.Tables[0]);
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<Model.ePioneer_Reservation> DataTableToList(DataTable dt)
        {
            List<Model.ePioneer_Reservation> modelList = new List<Model.ePioneer_Reservation>();
            int rowsCount = dt.Rows.Count;
            if (rowsCount > 0)
            {
                Model.ePioneer_Reservation model;
                for (int n = 0; n < rowsCount; n++)
                {
                    model = new Model.ePioneer_Reservation();
                    if (dt.Rows[n]["Id"] != null && dt.Rows[n]["Id"].ToString() != "")
                    {
                        model.Id = int.Parse(dt.Rows[n]["Id"].ToString());
                    }
                    if (dt.Rows[n]["UserId"] != null && dt.Rows[n]["UserId"].ToString() != "")
                    {
                        model.UserId = dt.Rows[n]["UserId"].ToString();
                    }
                    if (dt.Rows[n]["CourseInfoId"] != null && dt.Rows[n]["CourseInfoId"].ToString() != "")
                    {
                        model.CourseInfoId = int.Parse(dt.Rows[n]["CourseInfoId"].ToString());
                    }
                    if (dt.Rows[n]["CreateDate"] != null && dt.Rows[n]["CreateDate"].ToString() != "")
                    {
                        model.CreateDate = DateTime.Parse(dt.Rows[n]["CreateDate"].ToString());
                    }
                    if (dt.Rows[n]["Score"] != null && dt.Rows[n]["Score"].ToString() != "")
                    {
                        model.Score = int.Parse(dt.Rows[n]["Score"].ToString());
                    }
                    if (dt.Rows[n]["IsClose"] != null && dt.Rows[n]["IsClose"].ToString() != "")
                    {
                        model.IsClose = int.Parse(dt.Rows[n]["IsClose"].ToString());
                    }
                    if (dt.Rows[n]["Code"] != null && dt.Rows[n]["Code"].ToString() != "")
                    {
                        model.Code = dt.Rows[n]["Code"].ToString();
                    }
                    modelList.Add(model);
                }
            }
            return modelList;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetAllList()
        {
            return GetList("");
        }

        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        //public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        //{
        //return dal.GetList(PageSize,PageIndex,strWhere);
        //}

        #endregion  Method

        #region

        /// <summary>
        /// 新增预约信息
        /// </summary>
        /// <param name="id"></param>
        /// <param name="loginName"></param>
        /// <returns></returns>
        public int Add(string id, string loginName,string code)
        {

            DAL.ePioneer_UserInfo bll = new DAL.ePioneer_UserInfo();
            Model.ePioneer_UserInfo uModel = bll.Login(loginName);

            if (uModel != null)
            {
                List<Model.ePioneer_Reservation> list = GetModelList("UserId='" + uModel.Id + "' and CourseInfoId='" + id + "'");
                if (list.Count > 0)
                {
                    return -2;
                }
                Model.ePioneer_Reservation model = new Model.ePioneer_Reservation();
                model.CourseInfoId = Convert.ToInt32(id);
                model.UserId = uModel.Id.ToString();
                model.IsClose = 0;
                model.Score = 0;
                model.CreateDate = DateTime.Now;
                model.Code = code;
                if (Add(model) == 0)
                    return -1;
                else
                    return 0;
            }
            else
                return -1;

        }
        #endregion
    }
}
